$(function(){

    //加载页头
    $("#header").load("header.html",function(){
        $(".headerRight").find("li:nth-child(4)").addClass("active");
        //显示时间
        updateTime();
    });



    //1小时预报
    function oneHour(){
        var time=oneTime();

        v.pubTimeDisplay.val(time);
        var hour=time.slice(11,13);
        time=time.replace(/\//g,"").replace(" ","").replace(/:/g,"")+"00";

        var productcategory={
            time:time,
            hour:"60",
        };
        grid_controller(productcategory);

        var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/Disa_MST_GeologicalDisaster/getContourValidtimeListByTime?userid=wlzx&pwd=123&productcategory=grid_dzzh&fcstlevel=0&time='+time+'&timetype=D&admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';
        $.ajax({
            url:ajaxUrl,
            type:"get",
            success:function(data){
                if(data.DS.join("")){
                    $(".warning").addClass("displayNone")
                    drawAxisOne(parseInt(hour),1,data.DS);
                    removeAxis(productcategory);

                }else{
                    $(".warning").removeClass("displayNone")
                }

            }
        })
    }
    oneHour();

    //3小时预报
    function threeHour(){
        var time =threeTime();
        v.pubTimeDisplay.val(time);
        var hour=time.slice(11,13);
        var day=time.slice(8,10);
        time=time.replace(/\//g,"");
        var t1=(parseInt(hour)==20)?'12':"00";
        time=time.slice(0,8)+t1+'0000';
        var productcategory={
            time:time,
            hour:"180",
        };
        grid_controller(productcategory);
        var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/Disa_MST_GeologicalDisaster/getContourValidtimeListByTime?userid=wlzx&pwd=123&productcategory=grid_dzzh&fcstlevel=0&time='+time+'&timetype=D&admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';

        $.ajax({
            url:ajaxUrl,
            type:"get",
            success:function(data){
                if(data.DS.join("")){
                    $(".warning").addClass("displayNone");
                    drawAxisThree(parseInt(hour),3,day,5,2,data.DS);
                    removeAxis(productcategory);
                    console.log(data);
                }else{
                    $(".warning").removeClass("displayNone")
                }
            }
        })
    }

    //6小时降雨函数
    function sixHour(){
        var time =threeTime();
        v.pubTimeDisplay.val(time);
        var hour=time.slice(11,13);
        var day=time.slice(8,10);
        time=time.replace(/\//g,"");
        var t1=(parseInt(hour)==20)?'12':"00";
        time=time.slice(0,8)+t1+'0000';
        var productcategory={
            time:time,
            hour:"360",
        };

        grid_controller(productcategory);
        var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/Disa_MST_GeologicalDisaster/getContourValidtimeListByTime?userid=wlzx&pwd=123&productcategory=grid_dzzh&fcstlevel=0&time='+time+'&timetype=D&admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';

        $.ajax({
            url:ajaxUrl,
            type:"get",
            success:function(data){
                if(data.DS.join("")){
                    $(".warning").addClass("displayNone");
                    drawAxisSix(parseInt(hour),6,day,8,2,data.DS);
                    removeAxis(productcategory);
                    console.log(data);
                }else{
                    $(".warning").removeClass("displayNone")
                }


            }
        })
    }

    // 12小时降雨函数
    function twelveRain(){
        var time =threeTime();
        v.pubTimeDisplay.val(time);
        var hour=time.slice(11,13);
        var day=time.slice(8,10);
        time=time.replace(/\//g,"");
        var t1=(parseInt(hour)==20)?'12':"00";
        time=time.slice(0,8)+t1+'0000'
      var productcategory={
        time:time,
        hour:"720",
      };
      grid_controller(productcategory);
      var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/Disa_MST_GeologicalDisaster/getContourValidtimeListByTime?userid=wlzx&pwd=123&productcategory=grid_dzzh&fcstlevel=0&time='+time+'&timetype=D&admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';

      $.ajax({
        url:ajaxUrl,
        type:"get",
        success:function(data){
          if(data.DS.join("")){
            $(".warning").addClass("displayNone");
            drawAxisThree(parseInt(hour),12,day,12,8,data.DS);
            removeAxis(productcategory);
            console.log(data);
          }else{
            $(".warning").removeClass("displayNone")
          }
        }
      })
    }

    //24小时降雨函数
    function twentyFourHour(){
        var time =getCurrentTime();
        v.pubTimeDisplay.val(time);
        time=time.replace(/\//g,"").replace(" ","").replace(/:/g,"")+"00";
        var productcategory={
            time:time,
          hour:"1440",
        };
        grid_controller_24(productcategory);

      var ajaxUrl='http://10.135.30.116:8080/ZJDataCenter/api/Disa_MST_GeologicalDisaster/getContourValidtimeListByTime?userid=wlzx&pwd=123&productcategory=grid_dzzh&fcstlevel=0&time='+time+'&timetype=D&admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';
        $.ajax({
            url:ajaxUrl,
            type:"get",
            success:function(data){
                if(data.DS.join("")){
                    $(".warning").addClass("displayNone");
                    drawAxistwentyFour(data.DS);
                    removeAxis(productcategory)
                }else{
                    $(".warning").removeClass("displayNone");
                }

            }
        })
    }



    //获得当前数据
    function getCurrentTime(){
        var date=new Date();
        var day=date.getDate();
        var month=date.getMonth()+1;
        var hour=date.getHours();
        var minite=date.getMinutes();
        switch(true){
            case minite>50: minite=50;
                break;
            case minite>40: minite=40;
                break;
            case minite>30: minite=30;
                break;
            case minite>20: minite=20;
                break;
            case minite>10: minite=10;
                break;
            default :  minite="00";
                break;
        }
        hour=hour>9?hour:'0'+hour;
        month=month>9?month:'0'+month;
        day=day>9?day:'0'+day;
        return date.getFullYear()+'/'+month+'/'+day+' '+hour+":"+minite
    }


    //时间切换函数
    function removeAxis(productcategory){
        var index=0;
        v.changeTime.unbind();
        v.changeTime.on("click","button",function(e){
            e.preventDefault();
            var len=$(".changeTime ul li");
            if($(this).hasClass("changeNext")){
                $(this).siblings("button").prop("disabled",false);
                len.eq(index).addClass("displayNone").removeClass("displayBlock");
                len.eq(++index).addClass("active").siblings(".active").removeClass("active");
                productcategory.year=len.eq(index).attr('year');
                productcategory.hour=len.eq(index).attr("hour");
                grid_controller(productcategory);
                if(len.length-index<=8){$(this).prop("disabled",true)}
            }else{
                --index;
                $(this).siblings("button").prop("disabled",false);
                len.eq(index)
                len.eq(index).removeClass("displayNone").addClass("displayBlock").addClass("active").siblings(".active").removeClass("active")
                productcategory.year=len.eq(index).attr('year');
                productcategory.hour=len.eq(index).attr("hour");
                grid_controller(productcategory);
                if(index==0){
                    $(this).prop("disabled",true);
                }
            }
        })

        v.changeTime.on("mouseenter","li",function(){
            $(this).addClass("active").siblings(".active").removeClass("active");
            productcategory.year=$(this).attr('year');
            productcategory.hour=$(this).attr("hour");
            grid_controller(productcategory);

        });

    }

    //绘制1小时底部时间轴
    function drawAxisOne(initTime,step,data){
        var html="";
        var j=1;
        var date=new Date();
        for(var i=0;i<data.length;i++){
            var time=initTime+j*step;
            j++;
            html+='<li hour="'+data[i].hours+'" year="'+data[i].time+'">'+
                '<span class="upRect"></span>'+
                '<span>'+(time>23?"00":time)+':00</span>';
            if(time>23){
                initTime=1;
                j=0;
                date.setDate(date.getDate()+1)
                html+='<i ></i>'+
                    '<b class="changeDay">'+(date.getMonth()+1)+'/'+date.getDate()+'</b>'
            }
            html+='</li>'
        }
        v.timeAjax.html(html);
    }
    //绘制3小时底部时间轴
    function drawAxisThree(initTime,step,day,endT,num,data){
        var html="";
        var j=1;
        var date=new Date();
        date.setDate(day);
        for(var i=0;i<data.length;i++){
            var time=initTime+j*step;
            j++;
            html+='<li hour="'+data[i].hours+'" year="'+data[i].time+'">'+
                '<span class="upRect"></span>'+
                '<span>'+(time>23?num:time)+':00</span>'
            if(time>23){
                initTime=endT;
                j=0;
                date.setDate(date.getDate()+1)
                html+='<i ></i>'+
                    '<b class="changeDay">'+(date.getMonth()+1)+'/'+date.getDate()+'</b>'
            }
            html+='</li>'
        }
        v.timeAjax.html(html)
    }
    //绘制6小时底部时间轴
    function drawAxisTwelve(initTime,step,day,endT,num,data){
        var html="";
        var j=1;
        var time=null;
        var date=new Date();
        date.setDate(day);
        for(var i=0;i<data.length;i++){
            time=initTime+j*step;

                j++
                html+='<li hour="'+data[i].hours+'" year="'+data[i].time+'">'+
                    '<span class="upRect"></span>'+
                    '<span>'+(time>23?num:time)+':00</span>'
                if(time>23){
                    initTime=endT;
                    j=0;
                    date.setDate(date.getDate()+1)
                    html+='<i ></i>'+
                        '<b class="changeDay">'+(date.getMonth()+1)+'/'+date.getDate()+'</b>'
                }
                html+='</li>'

        }
        v.timeAjax.html(html);
            }
  //绘制6小时底部时间轴
  function drawAxisSix(initTime,step,day,endT,num,data){
    var html="";
    var j=1;
    var time=null;
    var date=new Date();
    date.setDate(day);
    for(var i=0;i<data.length;i++){
      time=initTime+j*step;

      j++
      html+='<li hour="'+data[i].hours+'" year="'+data[i].time+'">'+
        '<span class="upRect"></span>'+
        '<span>'+(time>23?num:time)+':00</span>'
      if(time>23){
        initTime=endT;
        j=0;
        date.setDate(date.getDate()+1)
        html+='<i ></i>'+
          '<b class="changeDay">'+(date.getMonth()+1)+'/'+date.getDate()+'</b>'
      }
      html+='</li>'

    }
    v.timeAjax.html(html);
  }
    //绘制24小时底部时间轴
    function drawAxistwentyFour(data){
        var date=new Date();
        for(var i=0,html="";i<data.length;i++){
                date.setDate(date.getDate()+1)
                html+='<li hour="'+data[i].hours+'" year="'+data[i].time+'">'+
                    '<span class="upRect"></span>'+
                    '<span>'+date.getDate()+'日</span>'
                if(date.getDate()==1){
                    date.setDate(date.getDate()+1)
                    html+='<i ></i>'+
                        '<b class="changeDay">'+(date.getMonth()+1)+'月</b>'
                }
                html+='</li>'
        }
        v.timeAjax.html(html)
    }


    //1小时发布时间函数
    function oneTime(){
        var date=new Date();
        var day=date.getDate();
        var month=date.getMonth()+1;
        var hour=date.getHours()-1;
        hour=hour>9?hour:'0'+hour;
        month=month>9?month:'0'+month;
        day=day>9?day:'0'+day;
        return date.getFullYear()+'/'+month+'/'+day+' '+hour+":00"
    }
    //3小时发布时间函数
    function threeTime(){
        var date=new Date();
        var Mouth = date.getMonth()>=9?date.getMonth()+1:"0"+(date.getMonth()+1);
        var Day = date.getDate()>9?date.getDate():"0"+date.getDate();
        var hour=date.getHours();
        if(parseInt(hour)<10){
            date.setDate(date.getDate()-1);
            hour="08";
        }else if(parseInt(hour)>=10&&parseInt(hour)<14){
            date.setDate(date.getDate()-1);
            hour="20";
        }else{
            hour="08";
        }
        return date.getFullYear()+"/"+Mouth+"/"+Day+"/"+hour+":00";
    }


    //加载地址灾害控件---1/3/6小时
    function grid_controller(productcategory){

      QXMap.removeVectorMapProduct(false);
        var mapProductOptions1 = {
            productcategory: 'grid_dzzh',
            time: productcategory.time,
            regioncode: '330000',
            method: 'Disa_MST_GeologicalDisaster',
            interface: 'getContourByTimeAndRegionCode',
            key: 'zjwlzx',
            pwd: '123',
            level: 0,
            validtime: productcategory.hour, //60 1小时降水预报,180 3小时降水预报,360 六小时降水预报
            gridmethod: 'Disa_MST_GeologicalDisaster',
            gridinterface: 'getGridInRectByTimeAndLevelAndValidtime',
            nlat: 55,
            slat: 18,
            wlon: 116,
            elon: 125,
            isshowzero: false,
            isshowcontrol: true,
            position: "topright",
            isforecast: true,
            isshowgridselect: false
        }
        var product1 = QXMap.addVectorMapProduct(mapProductOptions1, function (data) {

            if (data == null) return;
        });
        map.setZoom(10);
        map.setCenter({lng:120.570,lat:30.126});

    }
    //加载地址灾害控件---24小时
    function grid_controller_24(productcategory){
        var mapProductOptions = {
            productcategory: 'tmp2m',
            time: productcategory.time,
            regioncode: '330000',
            method: 'NAFP_FOR_FTM_ST24H_ZJ',
            interface: 'getContourEleByTimeAndRegionCode',
            key: 'zjwlzx',
            pwd: '123',
            level: 0,
            validtime: 6, //1-24
            gridmethod: 'NAFP_FOR_FTM_ST24H_ZJ',
            gridinterface: 'getGridForecastEleGridInRectByTimeAndLevelAndValidtime',
            nlat: 55,
            slat: 18,
            wlon: 116,
            elon: 125,
            isshowzero: false,
            isshowcontrol: true,
            position: "topright",
            isforecast: true,
            isshowgridselect:true,
        }
        var product = QXMap.addVectorMapProduct(mapProductOptions, function (data) {
            if (data == null) return;
            QXMap.showValueClassByType(31.0);
        });
    }


    v.console.find("ul").on("click","li",function(){
        var val=$(this).children("input").prop("checked",true).val();
        if(val==="oneHour")oneHour();
        else if(val==="threeHour")threeHour();
        else if(val==="sixHour")sixHour();
        else if(val==="twelveHour")twelveRain();
        else if(val==="twentyfourHour")twentyFourHour();
    })
})
